class EndarUserController < ApplicationController
	layout 'standard' 
	# list returns the information needed for user/list.rhtml
	def list
		@event = EndarEvent.find(params[:id])
			
		@user = EndarUser.find(:all, :conditions => ["event_id = ?", params[:id]])
	end
	# show is called for each item in @users in user/list.rhtml
	def show
		@user = EndarUser.find(params[:uid])
		@event = EndarEvent.find(params[:id])
		@user_times = EndarUserTime.find(:all, :conditions => ["user_id = ? AND is_free = ?", @user.id, 1])
		
		recipient = @user.name
		if (@user[:email_sent] != 1) && (@user[:name] != @event.admin)
			InviteUsers.deliver_link(recipient, @user, @event)
			@user[:email_sent] = 1
			@user.save
			
		end
		
	end
	
	# new returns the information needed for user/new.rhtml
	def new
		@user = EndarUser.new
		@user[:email_sent] = 0
		@event = EndarEvent.find(params[:id])
		@user.endar_user_times.build
		
	end
	
	def	add_user
		@user = EndarUser.new(params[:user])
		@event = EndarEvent.find(params[:id])
		@user[:event_id] = @event.id
		if @user.save
			redirect_to :controller => 'endar_user', :action => 'show', :id => @event.id, :uid=>@user.id
		end
	end
	
	def _user_time_form
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
		@user_time = EndarUserTime.new
	end
	
	def _user_time_form_add
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
		@user_time = EndarUserTime.new
	end
	
	def _user_time_ics
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
	end
	
	def _user_time_ics_add
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
	end
	
	def _user_time_url
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
	end
	
	def _user_time_url_add
		@event = EndarEvent.find(params[:id])
		@user = EndarUser.find(params[:uid])
	end
	
	# create is called when someone clicks the Create button on user/new.rhtml
	def create
		@user = EndarUser.new(params[:user])
		@event = EndarEvent.find(params[:id])
		@event_times = EndarEventTime.find(:all, :conditions => ["event_id = ?", @event.id])
		
		@user[:event_id] = @event.id
		
		
		
		if @user.save
			@user_times.each do |u|
				u.update_attributes("user_id" => @user.id)
			end
			@event_times.each do |e|
				@user_times.each do |u|
					if e.date_begin >= u.date_begin and e.date_end <= u.date_end
						e.update_attributes("count" => e.count + 1)
						break
					end
				end
			end
			
			
			redirect_to :controller => 'endar_event', :action => 'show', :id => @event.id
			
			#email user link here
			
		else
			redirect_to :action => 'error'
		end
	end
	
	# edit returns the information needed for user/edit.rhtml
	def edit
		@event = EndarEvent.find(params[:id])
		@times = EndarUserTime.find(:all)
	end
	# update is called when someone clicks the Update button on user/edit.rhtml
	def update
	end
	
	# delete will remove the user
	def delete
	end
	
	def signup
		case request.method
			when :post
				@user = EndarUser.new(params[:user])
				if @user.save
					redirect_to :controller => 'endar_user', :action => 'list'
				end
		end
	end
	
	def login
		if session[:user] = EndarUser.authenticate(params[:login], params[:password]) 
			redirect_to :controller => 'endar_user', :action => 'list'
		else
			redirect_to :controller => 'endar_user', :action => 'list'
		end
	end
	
	def logout
		reset_session
		redirect_to :controller => 'endar_user', :action => 'list'
	end
end
